﻿using S.Common.Enums.Archives;
using S.EarTag.Domain.Dto;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.EarTag.Infrastructure.Repository.Impl.EarTagRepository
{
    /// <summary>
    /// 耳标仓储接口
    /// </summary>
    public class EarTagModelRepository : BaseRepository<EarTagModel>,IEarTagModelRepository
    {
        private readonly SqlSugarClient _db;
        public EarTagModelRepository(SqlSugarClient db):base(db)
        {
            _db= db;
        }
        /// <summary>
        /// 耳标批量添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> BatchAddEarTag(List<EarTagModel> model)
        {
            var result = await _db.Insertable(model).ExecuteCommandAsync();
            return result>0;
        }
        /// <summary>
        /// 耳标批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> BatchRemoveEarTag(List<int> ids)
        {
            var result = await _db.Deleteable<EarTagModel>().In(ids).ExecuteCommandAsync();
            return result > 0;
        }
       
        /// <summary>
        /// 修改耳标状态为已使用
        /// </summary>
        /// <param name="EarTagId"></param>
        /// <returns></returns>
        public async Task<bool> UpdateEarTagState(string EarTag)
        {
            string sql = "UPDATE EarTagModel SET UseState = '已使用' WHERE EarTag ='" + EarTag + "'";
            var result = await _db.Ado.ExecuteCommandAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 查询耳标状态为已使用
        /// </summary>
        /// <returns></returns>
        public async Task<List<EarTagModel>> GetEarTag()
        {
            var query = await _db.Queryable<EarTagModel>().Where(a => a.UseState == "未使用" && a.BindState==true).ToListAsync();
            return query;
        }

        /// <summary>
        /// 查询耳标状态为未绑定电子耳号
        /// </summary>
        /// <returns></returns>
        public async Task<List<EarTagModelDto>> GetEarTagModels()
        {
            //var query =await _db.Queryable<EarTagModel>().Where(a => a.BindState == false).ToListAsync();
            //return query;
                string sql = "select EarTagId,EarTag from EarTagModel where BindState=0";
                var result = await _db.Ado.SqlQueryAsync<EarTagModelDto>(sql);
                return result;
        }
        /// <summary>
        /// 根据耳标Id修改状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateEarTag(int EarTagId)
        {
            string sql = "UPDATE EarTagModel SET BindState = 'true' WHERE EarTagId = " + EarTagId;
            var result = await _db.Ado.ExecuteCommandAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 多条件查询显示列表
        /// </summary>
        /// <param name="EarTag"></param>
        /// <param name="breedItem"></param>
        /// <param name="birthState"></param>
        /// <param name="BindState"></param>
        /// <returns></returns>
        public async Task<List<EarTagModel>> GetEarTagModelsList(string? EarTag, int BreedItem, string? BirthState, string? BindState)
        {
            var result = await _db.Queryable<EarTagModel>().Where(e =>
            (string.IsNullOrEmpty(EarTag) || e.EarTag.Contains(EarTag)) &&
            (BreedItem == 0 || e.BreedItem == (VarietyEnum)BreedItem) &&
            (string.IsNullOrEmpty(BirthState) || e.BirthState == BirthState) &&
            (string.IsNullOrEmpty(BindState) || e.BindState == BindState.ObjToBool()))
            .ToListAsync();
            return result;
        }
        /// <summary>
        /// 根据id查询反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EarTagModel> GetByIdEarTag(int id)
        {
            var result = await _db.Queryable<EarTagModel>().Where(a => a.EarTagId == id).SingleAsync();
            return result;
        }
        /// <summary>
        /// 根据ids执行批量查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<EarTagModel>> GetEarTagids(string? ids)
        {
            var result = await _db.Ado.SqlQueryAsync<EarTagModel>($"SELECT * FROM [dbo].[EarTagModel] WHERE EarTagId in({ids})");
            return result;
        }
    }
}